import Vue from 'vue'
import Router from 'vue-router'
import Login from './components/Login.vue'
import Home from './components/Home.vue'
import Welcome from './components/Welcome.vue'
import Users from './components/Users.vue'
import Rights from './components/Rights.vue'
import Roles from './components/Roles.vue'
import Cates from './components/Cates.vue'
import GoodsParamas from './components/GoodsParamas.vue'
import Goods from './components/Goods.vue'
import AddGoods from './components/AddGoods.vue'
import Order from './components/Order.vue'
import Reports from './components/Reports.vue'

Vue.use(Router)
const router = new Router({
    routes: [
        { path: '/', redirect: '/login' },
        { path: '/login', component: Login },
        {
            path: '/home',
            redirect: '/welcome',
            component: Home,
            children: [
                { path: '/welcome', component: Welcome },
                { path: '/users', component: Users },
                { path: '/rights', component: Rights },
                { path: '/roles', component: Roles },
                { path: '/categories', component: Cates },
                { path: '/params', component: GoodsParamas },
                { path: '/goods', component: Goods },
                { path: '/goods/add', component: AddGoods },
                { path: '/orders', component: Order },
                { path: '/reports', component: Reports }
            ]
        }
    ]
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
    // to and from are both route objects. must call `next`.
    console.log(to);
    if (to.path == '/login') {
        next()
    } else {
        if (sessionStorage.getItem('token')) {
            next()
        } else {
            next('/login')
        }
    }




})

export default router